/*
 * @Author: szx
 * @Date: 2022-05-08 14:32:04
 * @LastEditTime: 2022-05-08 14:45:48
 * @Description:
 * @FilePath: \leetcode\剑指offerII\117\117.js
 */
/**
 * @param {string[]} strs
 * @return {number}
 */
const numSimilarGroups = (strs) => {
    const n = strs.length,
        len = strs[0].length,
        visited = new Array(n).fill(false);
    let res = 0;
    const checkStrIsSimilar = (str1, str2) => {
        let number = 0;
        for (let i = 0; i < len; i++) {
            if (str1[i] !== str2[i]) {
                number++;
                if (number > 2) return false;
            }
        }
        return number === 2 || number === 0;
    };
    const dfs = (index) => {
        for (let i = 0; i < n; i++) {
            if (!visited[i] && checkStrIsSimilar(strs[index], strs[i])) {
                visited[i] = true;
                dfs(i);
            }
        }
    };
    for (let i = 0; i < n; i++) {
        if (!visited[i]) {
            visited[i] = true;
            dfs(i);
            res++;
        }
    }
    return res;
};
